#include "mainwindow.h"
#include "ui_mainwindow.h"



#include <QMessageBox>

#include <QtDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_serial(new QSerialPort),
    m_settings(new SettingsDialog),
    m_status(new QLabel)
{
    ui->setupUi(this);

    ui->statusBar->addWidget(m_status);

    fashionThread = new QThread();
    m_fashion = new FashionStarServo(parent, m_serial);
    m_fashion->moveToThread(fashionThread);
    connect(fashionThread, &QThread::finished, m_fashion, &QObject::deleteLater);
    fashionThread->start();

    connect(ui->actionSetupSerial, &QAction::triggered,
            m_settings, &SettingsDialog::show);
    connect(ui->actionOpenCloseSerial, &QAction::triggered,
            this, &MainWindow::onActionOpenCloseSerial);
    connect(m_settings, &SettingsDialog::applied,
            this, &MainWindow::onActionOpenCloseSerial);
    connect(ui->btnScanDevice, &QPushButton::clicked,
            this, &MainWindow::onBtnScanDivce);
    connect(ui->btnSet, &QPushButton::clicked,
            this, &MainWindow::onBtnSet);
    connect(ui->btnStart, &QPushButton::clicked, this, &MainWindow::onBtnStart);

    qRegisterMetaType<uint8_t>("uint8_t");
    qRegisterMetaType<uint16_t>("uint16_t");
    connect(this, &MainWindow::setAngle, m_fashion, &FashionStarServo::setAngle);
    connect(this, &MainWindow::startMoving, m_fashion, &FashionStarServo::onStartTimer);
    connect(this, &MainWindow::stopMoving, m_fashion, &FashionStarServo::onStopTimer);


//    connect(ui->dspinAngle, static_cast<void(QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
//            this, &MainWindow::onDSpinAngleChanged);


    ui->dspinAngle->setSuffix("°");
    ui->dspinAngle->setAccelerated(true);
}

MainWindow::~MainWindow()
{
    delete ui;
    delete  m_serial;
    delete  m_settings;
    fashionThread->quit();
    fashionThread->wait();
}


void MainWindow::onActionOpenCloseSerial()
{
    const SettingsDialog::Settings p = m_settings->settings();
    m_serial->setPortName(p.name);
    m_serial->setBaudRate(p.baudRate);
    m_serial->setDataBits(p.dataBits);
    m_serial->setParity(p.parity);
    m_serial->setStopBits(p.stopBits);
    m_serial->setFlowControl(p.flowControl);

    if (ui->actionOpenCloseSerial->text()=="打开串口")
    {
        if (m_serial->open(QIODevice::ReadWrite))
        {
            ui->actionOpenCloseSerial->setText("关闭串口");
            m_settings->cancel();
            ui->actionSetupSerial->setEnabled(false);
            showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
                              .arg(p.name).arg(p.strBaudRate).arg(p.strDataBits)
                              .arg(p.strParity).arg(p.strStopBits).arg(p.strFlowControl));
        }
        else
        {
            QMessageBox::critical(this, tr("Error"), m_serial->errorString());
            showStatusMessage(tr("Open error"));
        }
    }
    else
    {
        if (m_serial->isOpen())
        {
            m_serial->close();
        }
        ui->actionOpenCloseSerial->setText("打开串口");
        ui->actionSetupSerial->setEnabled(true);
        showStatusMessage(tr("Disconnected"));
    }
}



void MainWindow::onBtnScanDivce()
{
    ui->textBrowser->append("Scan Device...\r\n");
}

void MainWindow::onBtnSet()
{
    ui->textBrowser->append(QString("%1").arg(ui->dspinAngle->value()));
    for (uint8_t i=0; i<=20; i++)
    {
       emit setAngle(i, ui->dspinAngle->value(), 100, 0);
    }

    //m_fashion->setAngle(0x02, ui->dspinAngle->value(), 100, 0);
}

void MainWindow::onBtnStart()
{
    if (ui->btnStart->text()==tr("Start"))
    {
        ui->btnStart->setText(tr("Stop"));
        emit startMoving();
    }
    else
    {
        ui->btnStart->setText(tr("Start"));
        emit stopMoving();
    }
}


void MainWindow::showStatusMessage(const QString &message)
{
    m_status->setText(message);
}

